package com.ehotting.edsta.circuitDesign.uitls;

import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.eaf.redis.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CircuitThreadContext {

    @Autowired
    private RedisService redisService;

    protected static final Logger logger = LoggerFactory.getLogger(CircuitThreadContext.class);

    /**
     * 静态变量保证了在并发环境中ThreadLocal实例是唯一的
     */
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static  void set(String value) {
        if(StringUtils.isEmpty(value)){
            logger.error("Set in threadLocal object not should be null!");
        }
        threadLocal.set(value);
    }

    public static String get() {
        String value = threadLocal.get();
        if (value != null)
            return  value;
        return null;

    }
    public boolean isActive(){
        String value = threadLocal.get();
        String s = redisService.get(value);
        Boolean aBoolean = Boolean.valueOf(s);
        return aBoolean;
    }
    public static void remove() {
        threadLocal.remove();
    }
}
